<?php
namespace Home\Controller;
use Think\Controller;
class TaskController extends Controller {
    public function index(){
        $openid = I('openid');
        $task = M('task');
        $data = $task->where('openid="'.$openid.'"')->select();
        if(count($data) == 0){
            $data['count'] = 0;
            $this->ajaxReturn($data,'JSON');
        }
        $this->ajaxReturn($data,'JSON');
    }
    
    public function add(){
        $data['staic'] = 0;
        $title = I('title');
        $integral = I('integral');
        $iconsrc = I('iconsrc');
        $openid = I('openid');
        // $data['title'] = $title;
        // $data['integral'] = $integral;
        // $data['iconsrc'] = $iconsrc;
        // $data['openid'] = $openid;
        $task = M('task');
        $task->title = $title;
        $task->integral = $integral;
        $task->iconsrc = $iconsrc;
        $task->jf_state = 0;
        $task->openid = $openid;
        $result = $task->add();
        if($result){
            $data['staic'] = 1;
            $this->ajaxReturn($data,'JSON');
        }
        $this->ajaxReturn($data,'JSON');
    }

    public function upimage(){
        $openid = I('openid');
        $data['state'] = 1;
        $data['message'] = '上传成功';
        $upload = new \Think\Upload();
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath = './static/rwUpFile/';
        $upload->savePath = '/'.$openid.'/';
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $data['state'] = 0;
            $data['message'] = $upload->getError();
            $this->ajaxReturn($data,'JSON');
        }else{// 上传成功 获取上传文件信息
            foreach($info as $file){
                $imgurl = $file['savepath'].$file['savename'];
            }
            $data['imgurl'] = $imgurl;
            if(session('?imgurl')){
                $imgurls = session('imgurl').$imgurl;
                session('imgurl', $imgurls);
            }
            else{
                session('imgurl', $imgurl);
            }
        }
        $this->ajaxReturn($data,'JSON');
        
    }
    public function rwsubmit(){
        $data['state'] = 0;
        $data['message'] = '提交成功';
        $taskid = I('taskid');
        $texts = I('texts');
        $imgurl = session('imgurl');
        $taskInfo = M('taskInfo');
        $res = $taskInfo->where('taskid='.$taskid)->select();
        if(is_array($res) && count($res)>0){
            print_r($res);
        }
        else{
            $taskInfo->taskid = $taskid;
            $taskInfo->imgurl = $imgurl;
            $taskInfo->texts = $texts;
            $re = $taskInfo->add();
            if(!$re){
                $data['state'] = 0;
                $data['message'] = '上传数据库错误！';
                $this->ajaxReturn($data,'JSON');
            }
            $task = M('task');
            $task->id = $taskid;
            $task->jf_state = 1;
            $re2 = $task->save();
            if(!$re2){
                $data['state'] = 0;
                $data['message'] = '状态修改错误！';
                $this->ajaxReturn($data,'JSON');
            }
        }
        $this->ajaxReturn($data,'JSON');
    }
}